//
//  TopApiTraderates.m
//  yuimall_Iphone
//
//  Created by yuike6 yuike6 on 12-4-16.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "TopApiTraderates.h"
#import "header.h"

@implementation TopApiTraderates
@synthesize num_iid;
@synthesize seller_nick;

-(void) dealloc
{
    YRelease(num_iid)
    YRelease(seller_nick);
    
    [super dealloc];
}



- (id) init
{
    self = [super init];
    if (self) 
    {
        self.apiContent = TOP_API_TRADERATES_SEARCH_TEMPLATE;    
    }
    
    return self;
}

- (NSString *) sing
{
    NSArray * apiBody = [self.apiContent split:@"?"];
    if (!apiBody || [apiBody count] < 2) {
        return nil;
    }
    
    NSString * body = [apiBody objectAtIndex:1];
    
    NSArray * keyValues = [body split:@"&"];
    keyValues = [keyValues sortedArrayUsingSelector:@selector(compare:)];
    
    NSString * sing = [[[NSString alloc] initWithString:self.appSecret] autorelease];
    for (NSString * keyValue in keyValues)
    {
        if ([NSString isNilOrEmpty:keyValue]) 
        {
            continue;
        }                
        
        if ([keyValue isIncludeSubString:@"sign="]) {
            continue;
        }        
        
        sing = [sing stringByAppendingString:[keyValue replace:@"=" with:@""]];        
    }
    sing = [sing stringByAppendingString:self.appSecret];    
    
    sing = [sing md5Digest];
    sing = [sing upper];    
    return sing;
}

- (NSString *) topApiUrl
{
    
    self.apiContent = [self.apiContent replace:_PARAM_TOP_TIMESTAMP_ with:self.time];
    self.apiContent = [self.apiContent replace:_PARAM_TOP_APPKEY_ with:self.appKey];
    self.apiContent = [self.apiContent replace:_PARAM_TOP_METHOD_ with:TOP_API_TRADERATES_SEARCH];
    
    self.apiContent =[self.apiContent replace:_PARAM_TOP_PRODUCTID_ with:self.num_iid];  
    self.apiContent =[self.apiContent replace:_PARAM_TOP_SELLER_NICK with:self.seller_nick];  
    
    self.apiContent = [self.apiContent replace:_PARAM_TOP_SIGN_ with:[self sing] ];   
    return self.apiContent;
}



@end
